/*
 * gpio.h
 *
 * The gpio driver for SinhMicro's SINH51 platform.
 *
 * Copyright (C) 2024 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lixiang<lixiang@sinhmicro.com>
 * 
 * Encoding format: GB2312
 * Version: v1.2.2
 * Date: 2024-11-05
 */

#ifndef __HAL_GPIO_H__
#define __HAL_GPIO_H__

#include "hal-config.h"

#include "types.h"

/*
 * In order to optimize the code size, we MUST realize
 * the most of GPIO's API using Macro, please realize these APIs as below:
 * 1. enable input mode(For P0, P10~P14)
 *    GPIO_P0_IM_EN(bitmask)
 *    GPIO_P1_0_4_IM_EN(bitmask)
 *    @param[in]  bitmask, a bitmask for the port's I/Os(0 ~ 7),
 *                         if ANY I/O's bit is euqal to 1,
 *                         it will be set to input mode.
 * 2. enable output mode(For P0, P10~P14)
 *    GPIO_P0_OM_EN(bitmask)
 *    GPIO_P1_0_4_OM_EN(bitmask)
 *    ...
 * 3. enable high-z mode(For P0, P10~P14)
 *    GPIO_P0_HZ_EN(bitmask)
 *    GPIO_P1_0_4_HZ_EN(bitmask)
 *    ...
 * 4. output high (For P0, P10~P14)
 *    GPIO_P0_OUTPUT_HIGH(bitmask)
 *    GPIO_P1_0_4_OUTPUT_HIGH(bitmask)
 *    ...
 * 5. output low (For P0, P10~P14)
 *    GPIO_P0_OUTPUT_LOW(bitmask)
 *    GPIO_P1_0_4_OUTPUT_LOW(bitmask)
 *    ...
 * 6. input value get (For P0, P10~P14)
 *    GPIO_P0_INPUT_VAL_GET()
 *    GPIO_P1_0_4_INPUT_VAL_GET()
 *    ...
 *    return an 8-bit value for the port's I/Os(0 ~ 7)
 * 7. enable pull-up mode(For P0, P10~P13, P14)
 *    GPIO_P0_PU_EN(bitmask)
 *    GPIO_P1_0_3_PU_EN(bitmask)
 *    GPIO_P14_PU_EN(bitmask)
 *    @param[in]  bitmask, a bitmask for the port's I/Os(0 ~ 7),
 *                         if ANY I/O's bit is euqal to 1,
 *                         it will be set to pull-up mode.
 * 8. enable pull-down mode(For P0, P10~P13, P14)
 *    GPIO_P0_PD_EN(bitmask)
 *    GPIO_P1_0_3_PD_EN(bitmask)
 *    GPIO_P14_PD_EN(bitmask)
 *    ...
 * 9. enable no-pull mode(For P0, P10~P13, P14)
 *    GPIO_P0_NP_EN(bitmask)
 *    GPIO_P1_0_3_NP_EN(bitmask)
 *    GPIO_P14_NP_EN(bitmask)
 *    ...
 * 10. GPIO drive current config(For P0, P10~P14)
 *     GPIO_PXX_DRV_SOURCE_SET(x)
 *     GPIO_PXX_DRV_SINK_SET(x)
 *     @param[in] x drive Register value, can be 0,1,2,3 etc.
 *    ...
 * 11. GPIO anglog function force enable/disable(For P02, P03, P11, P12)
 *     GPIO_ENABLE_ANALOG_FORCE_XX
 *     GPIO_DISABLE_ANALOG_FORCE_XX
 */

#include "gpio/gpio-ss805x.h"		

/*
 * this function is used to initialize the GPIO driver.
 *
 * @param[in]  None
 */
void gpio_init(void);

/*
 * this function is used to set the I/O port's MFP.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 * @param[in]  mfp_val  MFP value, 0~3, or 0~7
 */
void gpio_mfp_set(uint8_t port, uint8_t num, uint8_t mfp_val);

/*
 * this function is used to set the GPIO to input mode.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 */
void gpio_input_mode_enable(uint8_t port, uint8_t num);

/*
 * this function is used to set the GPIO to output mode.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 */
void gpio_output_mode_enable(uint8_t port, uint8_t num);

/*
 * this function is used to set the GPIO to high-z mode.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 */
void gpio_highz_mode_enable(uint8_t port, uint8_t num);

/*
 * this function is used to set the GPIO to output high.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 */
void gpio_output_high(uint8_t port, uint8_t num);

/*
 * this function is used to set the GPIO to output low.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 */
void gpio_output_low(uint8_t port, uint8_t num);


/*
 * this function is used to get the GPIO's input value.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 * @return 1 or 0 for the input value
 */
bit_t gpio_input_value_get(uint8_t port, uint8_t num);

/*
 * this function is used to set the GPIO to pull-up mode.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 */
void gpio_pull_up_mode_enable(uint8_t port, uint8_t num);

/*
 * this function is used to set the GPIO to pull-down mode.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 */
void gpio_pull_down_mode_enable(uint8_t port, uint8_t num);

/*
 * this function is used to set the GPIO to no-pull mode.
 *
 * @param[in]  port     I/O's port, 0, 1.
 * @param[in]  num      I/O's num, 0~7
 */
void gpio_no_pull_mode_enable(uint8_t port, uint8_t num);


#endif /* __HAL_GPIO_H__ */
